// pages/menu/region/locationDevice/editDevice/editDevice.js
var app = getApp()
var verify = require('../../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    success:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var locationid = options.locationid
    var deviceid = options.deviceid
    var customname = options.customname
    var qrcodeurl = options.qrcodeurl
    var telephone = options.telephone
    if (telephone == "null"){
      telephone = ""
    }
    if (qrcodeurl == "null") {
      qrcodeurl = ""
    }
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      locationid: locationid,
      deviceid: deviceid,
      customname: customname,
      qrcodeurl: qrcodeurl,
      telephone: telephone,
    })
  },

  // 修改设备显示名
  editName: function (res) {
    console.log("editName", res.detail.value)
    this.setData({
      customname: res.detail.value
    })
  },
  // 修改热线电话
  editPhone: function (res) {
    console.log("editPhone", res.detail.value)
    this.setData({
      telephone: res.detail.value
    })
  },
// 修改屏二维码
  editCode: function (res) {
    console.log("editCode", res.detail.value)
    this.setData({
      qrcodeurl: res.detail.value
    })
  },

  // 编辑设备显示信息
  editDevice:function(){
    console.log("editDevice")
    var locationid = this.data.locationid
    var deviceid = this.data.deviceid
    var customname = this.data.customname
    var devicepicture = "1"
    var qrcodeurl = this.data.qrcodeurl
    var telephone = this.data.telephone
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    console.log("locationid = " + locationid)
    console.log("deviceid = " + deviceid)
    console.log("customname = " + customname)
    console.log("devicepicture = " + devicepicture)
    console.log("qrcodeurl = " + qrcodeurl)
    console.log("telephone = ", telephone)

    if (customname == "") {
      wx.showModal({
        title: '未填写设备显示名！',
        content: '请输入设备显示名！！',
      })
    } else if (telephone == "") {
      wx.showModal({
        title: '未填写热线电话！',
        content: '请输入街道门牌号！！',
      })
    } else if (qrcodeurl == "") {
      wx.showModal({
        title: '未填写屏二维码！',
        content: '请输入屏二维码！！',
      })
    } else {
      wx.request({
        url: baseUrl + 'location/' + locationid + '/device/' + deviceid+'/info?customName=' + customname + '&devicePicture=1&qrCodeUrl=' + qrcodeurl + '&telePhone=' + telephone,
        method: "put",
        header: {
          'AUTHORIZATION': token
        },
        success: function (res) {
          console.log("success", res)
          if (res.data.code == 60006) {
            verify.clearAll()
          } else if (res.data.code == 200) {
            wx.showToast({
              title: '修改成功!',
              duration: 3500
            })
            that.setData({
              success: true
            })
          } else {
            wx.showModal({
              title: '保存失败',
              content: res.data.message,
            })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})